# coding=UTF-8
"""
ZIP压缩文件破解程序加强版，用户可以自己指定想要破解的文
件和破解字典，多线程破解
"""
import zipfile
#import threading
import optparse
import time

def extractFile(zFile, password):
	try:
		print("try: %s" % password)
		zFile.extractall(pwd=password)
		return password
	except:
		pass

def main():
	parser = optparse.OptionParser("usage: python %prog -f <zipfile> -d <dictionary>")
	parser.add_option("-f", dest="zname", type="string", help="specify zip file")
	parser.add_option("-d", dest="dname", type="string", help="specify dictionary file")
	options, args = parser.parse_args()

	if options.zname == None or options.dname == None:
		print("Errno[0] Empty args!\nPlease use '-h' or '--help' to learn how to use this script.")
		exit(0)
	else:
		zname = options.zname
		dname = options.dname
		try:
			zFile = zipfile.ZipFile(zname)
			dFile = open(dname, "r")
		except Exception, e:
			print(str(e))
			exit(0)

	count = 0;
	start = time.clock()
	for line in dFile.readlines():
		password = line.strip('\n\r')
		passed = extractFile(zFile, password)
		count = count + 1
		if passed:
			print("Found Passwd: %s" % passed)
			break
		# t = threading.Thread(target=extractFile, args=(zFile, password))
		# t.start()
		
	end = time.clock()
	print("Try count: %d, use time: %f s" % (count, (end-start)))

if __name__ == "__main__":
	main()